<?php
/**
 * TemplateController.php
 * @author zohnの梦
 * @email  1171672036@qq.com
 * @date   2021/1/8
 */

namespace app\api\controller;


use app\api\validate\TemplateValidate;
use app\common\exception\DbException;
use app\common\exception\RequestException;
use app\common\model\TemplateModel;
use think\facade\Request;
use Utils\Responses;

class TemplateController
{
    /**
     * 数据列表
     *
     * @return \think\response\Json
     */
    public function lists()
    {
        $TemplateModel = new TemplateModel();
        $filters       = Request::post('filters', []);
        $page_size     = Request::post('pages', env('page.size', 10));

        if ($filters && is_array($filters)) {
            foreach ($filters as $name => $value) {
                switch ($name) {
                    case 'ids':
                        if ($value && is_array($value)) {
                            $TemplateModel = $TemplateModel->where('id', 'in', $value);
                        }
                        break;
                }
            }
        }

        $list  = $TemplateModel->paginate($page_size)->toArray();
        $data  = $list['data'];
        $pages = get_list_pages($list);

        return Responses::data(200, '请求成功', $data, compact($pages));
    }

    /**
     * 数据详情
     *
     * @return \think\response\Json
     */
    public function detail()
    {
        $validate = new TemplateValidate();
        $vResult  = $validate->scene(__FUNCTION__)->check(Request::post());
        if (!$vResult) {
            throw new RequestException(400, $validate->getError());
        }

        $id   = Request::post('id');
        $info = TemplateModel::where('id', '=', $id)->find();
        if (!$info) {
            throw new RequestException(404, '数据不存在');
        }

        return Responses::data(200, '请求成功', $info);
    }

    /**
     * 创建数据
     *
     * @return \think\response\Json
     */
    public function create()
    {
        $validate = new TemplateValidate();
        $vResult  = $validate->scene(__FUNCTION__)->check(Request::post());
        if (!$vResult) {
            throw new RequestException(400, $validate->getError());
        }

        $data           = [];
        $data['title']  = Request::post('title', '', 'trim,strip_tags,htmlspecialchars');
        $data['sort']   = Request::post('sort', 0, 'intval');
        $data['status'] = 2;

        try {
            $info = TemplateModel::create($data);
            if (!$info) {
                throw new DbException(500, '数据添加失败');
            }

            return Responses::data(200, '请求成功', ['id' => $info->id]);
        } catch (DbException $e) {
            return Responses::data(500, $e->getMessage());
        }
    }

    /**
     * 更新数据
     *
     * @return \think\response\Json
     */
    public function update()
    {
        $validate = new TemplateValidate();
        $vResult  = $validate->scene(__FUNCTION__)->check(Request::post());
        if ($vResult) {
            throw new RequestException(400, $validate->getError());
        }

        $id   = Request::post('id');
        $info = TemplateModel::where('id', '=', $id)->find();
        if (!$info) {
            throw new RequestException(404, '数据不存在');
        }

        $data          = [];
        $data['title'] = Request::post('title', '', 'trim,strip_tags,htmlspecialchars');
        $data['sort']  = Request::post('sort', 0, 'intval');

        try {

            $result = TemplateModel::where('id', '=', $id)->update($data);
            if (!$result) {
                throw new DbException(500, '数据更新失败');
            }

            return Responses::data(200, 'success');
        } catch (DbException $e) {
            return Responses::data(500, $e->getMessage());
        }
    }

    /**
     * 删除数据
     *
     * @return \think\response\Json
     */
    public function delete()
    {
        $validate = new TemplateValidate();
        $vResult  = $validate->scene(__FUNCTION__)->check(Request::post());
        if (!$vResult) {
            throw new RequestException(400, $validate->getError());
        }

        $id = Request::post('id');
        try {
            TemplateModel::where('id', '=', $id)->delete();
        } catch (DbException $e) {
            throw new DbException(500, '删除失败');
        }

        return Responses::data(200, '删除成功');
    }
}
